<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.log &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-log">
            
  <h1>Source code for django.utils.log</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">mail</span>
<span class="kn">from</span> <span class="nn">django.core.mail</span> <span class="kn">import</span> <span class="n">get_connection</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInNextVersionWarning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="kn">import</span> <span class="n">import_string</span>
<span class="kn">from</span> <span class="nn">django.views.debug</span> <span class="kn">import</span> <span class="n">ExceptionReporter</span><span class="p">,</span> <span class="n">get_exception_reporter_filter</span>

<span class="c"># Imports kept for backwards-compatibility in Django 1.7.</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">NullHandler</span>  <span class="c"># NOQA</span>
<span class="kn">from</span> <span class="nn">logging.config</span> <span class="kn">import</span> <span class="n">dictConfig</span>  <span class="c"># NOQA</span>

<span class="n">getLogger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span>

<span class="c"># Default logging for Django. This sends an email to the site admins on every</span>
<span class="c"># HTTP 500 error. Depending on DEBUG, all other log records are either sent to</span>
<span class="c"># the console (DEBUG=True) or discarded by mean of the NullHandler (DEBUG=False).</span>
<span class="n">DEFAULT_LOGGING</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">&#39;version&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
    <span class="s">&#39;disable_existing_loggers&#39;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
    <span class="s">&#39;filters&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;require_debug_false&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;()&#39;</span><span class="p">:</span> <span class="s">&#39;django.utils.log.RequireDebugFalse&#39;</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="s">&#39;require_debug_true&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;()&#39;</span><span class="p">:</span> <span class="s">&#39;django.utils.log.RequireDebugTrue&#39;</span><span class="p">,</span>
        <span class="p">},</span>
    <span class="p">},</span>
    <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;console&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;INFO&#39;</span><span class="p">,</span>
            <span class="s">&#39;filters&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;require_debug_true&#39;</span><span class="p">],</span>
            <span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&#39;logging.StreamHandler&#39;</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="s">&#39;null&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&#39;logging.NullHandler&#39;</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="s">&#39;mail_admins&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;ERROR&#39;</span><span class="p">,</span>
            <span class="s">&#39;filters&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;require_debug_false&#39;</span><span class="p">],</span>
            <span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&#39;django.utils.log.AdminEmailHandler&#39;</span>
        <span class="p">}</span>
    <span class="p">},</span>
    <span class="s">&#39;loggers&#39;</span><span class="p">:</span> <span class="p">{</span>
        <span class="s">&#39;django&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;console&#39;</span><span class="p">],</span>
        <span class="p">},</span>
        <span class="s">&#39;django.request&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;mail_admins&#39;</span><span class="p">],</span>
            <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;ERROR&#39;</span><span class="p">,</span>
            <span class="s">&#39;propagate&#39;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="s">&#39;django.security&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;mail_admins&#39;</span><span class="p">],</span>
            <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;ERROR&#39;</span><span class="p">,</span>
            <span class="s">&#39;propagate&#39;</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span>
        <span class="p">},</span>
        <span class="s">&#39;py.warnings&#39;</span><span class="p">:</span> <span class="p">{</span>
            <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;console&#39;</span><span class="p">],</span>
        <span class="p">},</span>
    <span class="p">}</span>
<span class="p">}</span>


<span class="k">def</span> <span class="nf">configure_logging</span><span class="p">(</span><span class="n">logging_config</span><span class="p">,</span> <span class="n">logging_settings</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">sys</span><span class="o">.</span><span class="n">warnoptions</span><span class="p">:</span>
        <span class="c"># Route warnings through python logging</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">captureWarnings</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
        <span class="c"># RemovedInNextVersionWarning is a subclass of DeprecationWarning which</span>
        <span class="c"># is hidden by default, hence we force the &quot;default&quot; behavior</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&quot;default&quot;</span><span class="p">,</span> <span class="n">RemovedInNextVersionWarning</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">logging_config</span><span class="p">:</span>
        <span class="c"># First find the logging configuration function ...</span>
        <span class="n">logging_config_func</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="n">logging_config</span><span class="p">)</span>

        <span class="n">logging_config_func</span><span class="p">(</span><span class="n">DEFAULT_LOGGING</span><span class="p">)</span>

        <span class="c"># ... then invoke it with the logging settings</span>
        <span class="k">if</span> <span class="n">logging_settings</span><span class="p">:</span>
            <span class="n">logging_config_func</span><span class="p">(</span><span class="n">logging_settings</span><span class="p">)</span>


<div class="viewcode-block" id="AdminEmailHandler"><a class="viewcode-back" href="../../../topics/logging.html#django.utils.log.AdminEmailHandler">[docs]</a><span class="k">class</span> <span class="nc">AdminEmailHandler</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">Handler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;An exception log handler that emails log entries to site admins.</span>

<span class="sd">    If the request is passed as the first argument to the log record,</span>
<span class="sd">    request data will be provided in the email report.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">include_html</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">email_backend</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">Handler</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">include_html</span> <span class="o">=</span> <span class="n">include_html</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">email_backend</span> <span class="o">=</span> <span class="n">email_backend</span>

    <span class="k">def</span> <span class="nf">emit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">record</span><span class="o">.</span><span class="n">request</span>
            <span class="n">subject</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s"> IP): </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">record</span><span class="o">.</span><span class="n">levelname</span><span class="p">,</span>
                <span class="p">(</span><span class="s">&#39;internal&#39;</span> <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">)</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">INTERNAL_IPS</span>
                 <span class="k">else</span> <span class="s">&#39;EXTERNAL&#39;</span><span class="p">),</span>
                <span class="n">record</span><span class="o">.</span><span class="n">getMessage</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="nb">filter</span> <span class="o">=</span> <span class="n">get_exception_reporter_filter</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="n">request_repr</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">{0}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="nb">filter</span><span class="o">.</span><span class="n">get_request_repr</span><span class="p">(</span><span class="n">request</span><span class="p">)))</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="n">subject</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">record</span><span class="o">.</span><span class="n">levelname</span><span class="p">,</span>
                <span class="n">record</span><span class="o">.</span><span class="n">getMessage</span><span class="p">()</span>
            <span class="p">)</span>
            <span class="n">request</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="n">request_repr</span> <span class="o">=</span> <span class="s">&quot;unavailable&quot;</span>
        <span class="n">subject</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">format_subject</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">record</span><span class="o">.</span><span class="n">exc_info</span><span class="p">:</span>
            <span class="n">exc_info</span> <span class="o">=</span> <span class="n">record</span><span class="o">.</span><span class="n">exc_info</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">exc_info</span> <span class="o">=</span> <span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">record</span><span class="o">.</span><span class="n">getMessage</span><span class="p">(),</span> <span class="bp">None</span><span class="p">)</span>

        <span class="n">message</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="se">\n\n</span><span class="s">Request repr(): </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">record</span><span class="p">),</span> <span class="n">request_repr</span><span class="p">)</span>
        <span class="n">reporter</span> <span class="o">=</span> <span class="n">ExceptionReporter</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">is_email</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="o">*</span><span class="n">exc_info</span><span class="p">)</span>
        <span class="n">html_message</span> <span class="o">=</span> <span class="n">reporter</span><span class="o">.</span><span class="n">get_traceback_html</span><span class="p">()</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">include_html</span> <span class="k">else</span> <span class="bp">None</span>
        <span class="n">mail</span><span class="o">.</span><span class="n">mail_admins</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                         <span class="n">html_message</span><span class="o">=</span><span class="n">html_message</span><span class="p">,</span>
                         <span class="n">connection</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">connection</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">backend</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">email_backend</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">format_subject</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subject</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Escape CR and LF characters, and limit length.</span>
<span class="sd">        RFC 2822&#39;s hard limit is 998 characters per line. So, minus &quot;Subject: &quot;</span>
<span class="sd">        the actual subject must be no longer than 989 characters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">formatted_subject</span> <span class="o">=</span> <span class="n">subject</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">n&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\r</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">r&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">formatted_subject</span><span class="p">[:</span><span class="mi">989</span><span class="p">]</span>

</div>
<div class="viewcode-block" id="CallbackFilter"><a class="viewcode-back" href="../../../topics/logging.html#django.utils.log.CallbackFilter">[docs]</a><span class="k">class</span> <span class="nc">CallbackFilter</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">Filter</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A logging filter that checks the return value of a given callable (which</span>
<span class="sd">    takes the record-to-be-logged as its only parameter) to decide whether to</span>
<span class="sd">    log a record.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">callback</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">callback</span> <span class="o">=</span> <span class="n">callback</span>

    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">record</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="mi">0</span>

</div>
<div class="viewcode-block" id="RequireDebugFalse"><a class="viewcode-back" href="../../../topics/logging.html#django.utils.log.RequireDebugFalse">[docs]</a><span class="k">class</span> <span class="nc">RequireDebugFalse</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">Filter</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span>

</div>
<div class="viewcode-block" id="RequireDebugTrue"><a class="viewcode-back" href="../../../topics/logging.html#django.utils.log.RequireDebugTrue">[docs]</a><span class="k">class</span> <span class="nc">RequireDebugTrue</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">Filter</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">record</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.log</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>